import express from 'express'
import fs from 'fs'
const app = express.Router();

// 方式1 下载文件
app.get('/download1/*',  (req, res, next) => {
  var name = 'db.json';
  var path = './' + name;
  var size = fs.statSync(path).size;
  var f = fs.createReadStream(path);
  res.writeHead(200, {
    'Content-Type': 'application/force-download',
    'Content-Disposition': 'attachment; filename=' + name,
    'Content-Length': size
  });
  f.pipe(res);
});


// 方式2 下载文件
app.post('/download2',(req,res)=>{
  // let { filename,mimeType } = req.body;
  let { filename,mimeType } = req.query;
  // var path="public/pdf/5ea56710fb5a2fdad7cabcf3.pdf";
  // filename = '02cbc5d9-e10f-4f8d-8fae-304cc609a8f0';  //pdf
  // filename = '6f3db1f9-e247-4aa5-bca7-93bae62a0079' // mp4
  // filename = '6a908990-0c4f-4101-9e9d-4c8b00a8133b' // doc
  // filename='2055c73b-1b37-40bc-ba50-b6b8ef82e63c' // docx
  // mimeType='application/pdf';

  let type = getType(mimeType);  //获取文件后缀名
  console.log(filename,mimeType,type);

  // path = path.join(__dirname, '../public/showPdf/'+filename+'.'+type);
  path='./public/showPdf/'+filename+'.'+type;
  // path="C:\Users\sswc\Desktop\gai2\public\showPdf\02cbc5d9-e10f-4f8d-8fae-304cc609a8f0.pdf"
  console.log(path);


  var fileStream = fs.createReadStream(path);
  var size = fs.statSync(path).size;
  res.setHeader('Content-Type', mimeType);
  res.setHeader('Content-Disposition', `attachment;filename=${filename}.${type}`);
  res.setHeader('Content-Length', size);
  fileStream.on('data', function (data) {
      res.write(data, 'binary');
  });
  fileStream.on('end', function () {
      res.end();
      console.log('The file has been downloaded successfully!');
  });
   
})

// 方式3 下载文件
app.post('/download3',(req,res)=>{
  // let { filename,mimeType } = req.body;
  let { filename,mimeType } = req.query;
  // var path="public/pdf/5ea56710fb5a2fdad7cabcf3.pdf";
  // filename = '02cbc5d9-e10f-4f8d-8fae-304cc609a8f0';  //pdf
  // filename = '6f3db1f9-e247-4aa5-bca7-93bae62a0079' // mp4
  // filename = '6a908990-0c4f-4101-9e9d-4c8b00a8133b' // doc
  // filename='2055c73b-1b37-40bc-ba50-b6b8ef82e63c' // docx
  // mimeType='application/pdf';

  let type = getType(mimeType);  //获取文件后缀名
  console.log(filename,mimeType,type);

  // path = path.join(__dirname, '../public/showPdf/'+filename+'.'+type);
  path='./public/showPdf/'+filename+'.'+type;
  // path="C:\Users\sswc\Desktop\gai2\public\showPdf\02cbc5d9-e10f-4f8d-8fae-304cc609a8f0.pdf"
  console.log(path);


  fs.readFile(path, (err, data) => {
      if (err) {
        throw err
      }
      res.setHeader('Content-Type',mimeType)
      // 设置文件名
      // Content-disposition 是 MIME 协议的扩展，MIME 协议指示 MIME 用户代理如何显示附加的文件
      // attachment 以附件形式下载
      res.setHeader('Content-Disposition', `attachment; filename=${filename}.${type}`)
      // 返回数据
      res.end(data)
  })

})

// 获取文件得后缀名
function getType(mimeType){
  switch(mimeType) {
      case 'video/x-msvideo':  return 'avi';
      case 'video/x-ms-wmv': return 'wmv';
      case 'video/x-flv': return 'flv';
      case 'image/x-ico': return 'ico';
      case 'image/x-psd': return 'psd';
      case 'text/csv': return 'csv';
      case 'text/html': return 'html';
      case 'text/plain': return 'txt';
      case 'text/xml': return 'xml';
      case 'video/mp4': return 'mp4';
      case 'video/mpeg': return 'mpg';
      case 'application/x-shockwave-flash': return 'swf';
      case 'audio/mpeg': return 'mp3';
      case 'audio/x-ogg': return 'ogg';
      case 'audio/x-wav': return 'wav';
      case 'image/jpeg': return 'jpg';
      case 'image/png': return 'png';
      case 'image/svg+xml': return 'svg';
      case 'application/x-mspublisher': return 'pub';
      case 'application/vnd.visio': return 'vsd';
      case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': return 'docx';
      case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': return 'xlsx';
      case 'application/vnd.openxmlformats-officedocument.presentationml.presentation': return 'pptx';
      case 'application/msword': return 'doc';
      case 'application/pdf': return 'pdf';
      case 'application/postscript': return 'ps';
      case 'application/rtf': return 'rtf';
      case 'application/vnd.ms-access': return 'mdb';
      case 'application/vnd.ms-excel': return 'xls';
      case 'application/vnd.ms-powerpoint': return 'ppt';
      case 'application/vnd.oasis.opendocument.text': return 'ott';
  }
}
module.exports = app;